<!DOCTYPE html>
<html xmlns:shiro="http://www.w3.org/1999/xhtml">
<head xmlns:th="http://www.thymeleaf.org" >
	<meta charset="UTF-8">
	<title>角色管理</title>
	<link th:include="/comm/header :: header"/>
	<script type="text/javascript">
		function formatOp(val,row,index){
			var str = '';
			if ($("#rolemenuset").length > 0) {
				str += $.formatString('<a  href="javascript:void(0)" class="res-easyui-linkbutton-edit success"  onclick="menuSetFun(\'{0}\');" >资源设置</a>', row.id);
			}
			if ($("#roleupdate").length > 0) {
				str += $.formatString('<a href="javascript:void(0)" class="role-easyui-linkbutton-edit warning"  onclick="editRoleFun(\'{0}\');" >编辑</a>', index);
			}
			if ($("#roledelete").length > 0) {
				str += $.formatString('<a href="javascript:void(0)" class="role-easyui-linkbutton-del error"   onclick="deleteRoleFun(\'{0}\');" >删除</a>', row.id);
			}
			return str;
		}

		function menuSetFun(roleId){
			$("#dlg2").dialog("open").dialog("setTitle","权限菜单设置");
			$("#menuTree").tree({
				lines:true,
				checkbox:true,
				method:'get',
				cascadeCheck:false,
				url:'/sys/role/getres?id='+roleId,
				onLoadSuccess:function(){
					$("#menuTree").tree("expandAll");
				},
				onCheck:function(node,checked){
					if(checked){
						checkNode($("#menuTree").tree("getParent",node.target));
					}
				}
			});
			$("#roleId").val(roleId);
		}

		function checkNode(node){
			if(!node){
				return;
			}else{
				checkNode($("#menuTree").tree("getParent",node.target));
				$("#menuTree").tree("check",node.target);
			}
		}

		function saveMenuSet(){
			var roleId=$("#roleId").val();
			var nodes=$("#menuTree").tree("getChecked");
			var menuArrIds=[];
			for(var i=0;i<nodes.length;i++){
				menuArrIds.push(nodes[i].id);
			}
			var menuIds=menuArrIds.join(",");
			$.post("/sys/role/saveres",{menuIds:menuIds,roleId:roleId},function(result){
				if(result.flag){
					message("保存成功")
					closeMenuSetDialog();
				}else{
					message("提交失败，请联系管理员");
				}
			},"json");
		}

		function closeMenuSetDialog(){
			$("#dlg2").dialog("close");
		}

		function searchRole(){
			$("#dg").datagrid('load',{
				"rolename":$("#s_roleName").val()
			});
		}
		function closeRoleDialog(){
			$("#dlg").dialog("close");
			resetValue();
		}

		function saveRole(){
			$("#fm").form("submit",{
				url:"/sys/role/save",
				onSubmit:function(){
					return $(this).form("validate");
				},
				success:function(result){
					var result=eval('('+result+')');
					if(result.flag){
						message("保存成功 ！");

						$("#dg").datagrid("reload");
						$("#dlg").dialog("close");

					}else{
						message(result.message);
					}
				}
			});
		}

		function openRoleAddDialog(){
			$("#dlg").dialog("open").dialog("setTitle","添加角色信息");
			$('#fm').form('clear');
		}

		function editRoleFun(index){
			$("#dg").datagrid('selectRow',index);
			var row =$("#dg").datagrid('getSelected');
			$("#dlg").dialog("open").dialog("setTitle","修改角色信息");
			$("#fm").form("load",row);
		}
		function deleteRoleFun(rid) {
			$.messager.confirm("系统提示","您确定要删除这条数据吗?",function(r){
				if(r){
					$.post("/sys/role/delete",{id:rid},function(result){
						if(result.flag){
							message("删除成功！");
							$("#dg").datagrid("reload");
						}else{
							message(result.message)
						}
					},"json");
				}
			});
		}
		function deleteRoles(){
			var selectedRows=$("#dg").datagrid("getSelections");

			if(selectedRows.length<1){
				message("请选择一条要删除的数据！");
				return;
			}
			var ids=selectedRows.map(function (role) {
				return role.id;
			})
			var roids=ids.join(",");
			/*		//装进数组
                    var para=[];
                    for (var i=0;i<selectedRows.length;i++){
                        para.push(selectedRows[i].rid);
                    }
                    //装json
                    var params = {
                        rid:para
                    };
                    //传数组必须序列化
                    var ps = $.param(params, true);
                    "/sys/role/del",ps,function(result)*/
			$.messager.confirm("系统提示","您确定要删除这条数据吗?",function(r){
				if(r){
					$.post("/sys/role/delall",{rids:roids},function(result){
						if(result.success){
							message("删除成功！");
							$("#dg").datagrid("reload");
						}else{
							message(result.info)
						}
					},"json");
				}
			});
		}


	</script>
</head>
<body style="margin: 1px">
<shiro:hasPermission name="sys:role:set">
	<input type="hidden" id="rolemenuset"/>
</shiro:hasPermission>
<shiro:hasPermission name="sys:role:delete">
	<input type="hidden" id="roledelete"/>
</shiro:hasPermission>
<shiro:hasPermission name="sys:role:update">
	<input type="hidden" id="roleupdate"/>
</shiro:hasPermission>
<table id="dg" title="角色管理" class="easyui-datagrid"
	   fitColumns="true" pagination="true" rownumbers="true" singleSelect="false"
	   url="/sys/role/list" fit="true" toolbar="#tb" method="get">
	<thead>

	<th data-options="field:'ck',checkbox:true"></th>
	<th field="id" width="20" align="center" hidden="true">编号</th>
	<th field="cname" width="50" align="center">角色名称</th>
	<th field="remark" width="100" align="center">备注</th>
	<th field="op" width="50" align="center" formatter="formatOp">操作</th>
	</thead>
</table>

<div id="tb">
	<div style="display: inline-block;">
		<shiro:hasPermission name="sys:role:save">
			<a href="javascript:openRoleAddDialog()" class="easyui-linkbutton primary"  iconCls="fa fa-plus" plain="true">添加</a>
		</shiro:hasPermission>
		<shiro:hasPermission name="sys:role:delete">
			<a href="javascript:deleteRoles()" class="easyui-linkbutton error" iconCls="fa fa-trash" plain="true">批量删除</a>
		</shiro:hasPermission>

	</div>
	<div style="display: inline-block;float: right;font-size: 18px;text-align: center">
		&nbsp;角色名称：&nbsp;<input type="text" id="s_roleName" class="easyui-validatebox easyui-textbox"  data-options="validType:'length[3,10]',iconAlign:'left', iconCls:'fa fa-user'" size="20" onkeydown="if(event.keyCode==13) searchRole()"/>
		<a href="javascript:searchRole()" class="easyui-linkbutton primary" iconCls="fa fa-search" plain="true">搜索</a>
	</div>
</div>


<div id="dlg" class="easyui-dialog" style="width: 450px;height: 250px;padding: 10px 20px"
	 closed="true" buttons="#dlg-buttons">
	<form id="fm" method="post">
		<input type="hidden" name="id" />
		<table cellspacing="8px">
			<tr>
				<td>角色名称：</td>
				<td>
					<input type="text" id="name" name="cname" class="easyui-validatebox easyui-textbox" style="height: 35px;width: 200px" required="true"/>
				</td>
			</tr>
			<tr>
				<td valign="top">备注：</td>
				<td>
					<textarea rows="5" cols="40" id="remark" name="remark"></textarea>
				</td>
			</tr>
		</table>
	</form>
</div>

<div id="dlg-buttons">
	<a href="javascript:saveRole()" class="easyui-linkbutton" iconCls="fa fa-check" >保存</a>
	<a href="javascript:closeRoleDialog()" class="easyui-linkbutton" iconCls="fa fa-times" >关闭</a>
</div>


<div id="dlg2" class="easyui-dialog" style="width: 300px;height: 500px;padding: 10px 20px"
	 closed="true" buttons="#dlg-buttons2">
	<input  id="roleId" name="roleId"/>
	<ul id="menuTree" class="easyui-tree"></ul>
</div>

<div id="dlg-buttons2">
	<a href="javascript:saveMenuSet()" class="easyui-linkbutton" iconCls="fa fa-check" >保存</a>
	<a href="javascript:closeMenuSetDialog()" class="easyui-linkbutton" iconCls="fa fa-times" >关闭</a>
</div>
<script type="text/javascript">
	$(function () {
		$("#dg").datagrid({
			onLoadSuccess: function (data) {
				$('.res-easyui-linkbutton-edit').linkbutton({text: '资源设置', plain: true, iconCls: 'fa fa-wrench'});
				$('.role-easyui-linkbutton-edit').linkbutton({text: '编辑', plain: true, iconCls: 'fa fa-paint-brush'});
				$('.role-easyui-linkbutton-del').linkbutton({text: '删除', plain: true, iconCls: 'fa fa-trash'});
				$('#dg').datagrid('fixRowHeight');
			}
		});
	})
</script>
</body>
</html>